package com.qf.anno.conf;

import com.qf.anno.domain.AccountLog;
import com.qf.anno.domain.User;
import com.qf.anno.mapper.AccountLogMapper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;

@Component
@Aspect
public class DeleteAdvice {

    @Autowired
    private AccountLogMapper accountLogMapper;

    @Autowired
    private HttpServletRequest request;

    @Pointcut("execution(* com.qf.anno.service..*.delete*(..))")
    public void pt(){}

    @Around("pt()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {

        //删除操作
        Object proceed = pjp.proceed();

        //记录日志（这个记录不要影响删除事务）
        System.out.println("记录日志....");
        AccountLog accountLog = new AccountLog();
        accountLog.setOperation("delete");
        accountLog.setOperation_time(new Date());
        Integer uid = ((User) request.getSession().getAttribute("userInfo")).getId();
        accountLog.setOperation_id(uid);
        accountLog.setOperation_params(Arrays.toString(pjp.getArgs()));

        accountLogMapper.insert(accountLog);

        return proceed;
    }
}
